Awards = {}
Awards.List = {}
Awards.Final = {}
Awards.Conditions = {}

HighToLow = function(a,b) return a > b end
LowToHigh = function(a,b) return a < b end

function Awards.Calculate()
	if CLIENT then return end
	for k,v in pairs(Awards.List) do
		local condition = true
		if v.Condition then
			condition = v.Condition(1)
		end
		if condition == false then
			for c,d in pairs(player.GetAll()) do
				umsg.Start("AwardWinner", d)
				umsg.String(v.Name)
				umsg.String(v.Desc)
				umsg.String("Nobody!")
				umsg.String(v.Sound)
				umsg.Short(0)
				umsg.End()
			end
		else
			local tbl = {}
			for c,d in pairs(player.GetAll()) do
				tbl[c] = d:GetAwardPoints(v.Name,0)
			end
			table.sort(tbl, v.CountStyle)
			local person, score
			for c,d in pairs(player.GetAll()) do
				if tbl[1] == d:GetAwardPoints(v.Name,0) then
					score = tbl[1]
					person = d
				end
			end
			for c,d in pairs(player.GetAll()) do
				umsg.Start("AwardWinner", d)
				umsg.String(v.Name)
				umsg.String(v.Desc)
				umsg.String(person:Name() or "Nobody!")
				umsg.String(v.Sound)
				umsg.Short(score)
				umsg.End()
			end
		end
	end
	for k,v in pairs(player.GetAll()) do
		umsg.Start("AwardsEnd", v)
		umsg.End()
	end
end 

function Awards.AddToList(tbl)
	local num = table.Count(Awards.List) + 1
	Awards.List[num] = tbl
end

Awards.Conditions.Medic = function(x)
	for k,v in pairs(player.GetAll()) do
		if v.HumanClass == "Medic" then
			return true
		end
	end
	return false
end

Awards.Conditions.Engineer = function(x)
	for k,v in pairs(player.GetAll()) do
		if v.HumanClass == "Engineer" then
			return true
		end
	end
	return false
end

Awards.Conditions.Support = function(x)
	for k,v in pairs(player.GetAll()) do
		if v.HumanClass == "Support" then
			return true
		end
	end
	return false
end

Awards.Conditions.Zomb = function(x)
	return team.NumPlayers(TEAM_UNDEAD) > 0
end

//You can fuck with the award names here.
AWARD_GOOD_POINTS = "Big Spender" 
AWARD_GOOD_KILLS = "Zombie Slayer" 
AWARD_GOOD_HEADSHOT = "Grey Matter" 
AWARD_GOOD_DISTANCE = "Marksman"
AWARD_GOOD_ENGINEER = "Lumberjack" 
AWARD_GOOD_SUPPORT = "Team Player" 
AWARD_GOOD_MEDIC = "Medicine Man" 
AWARD_GOOD_ITEMS = "Shoplifter" 
AWARD_GOOD_PYRO = "Pyromaniac"
AWARD_GOOD_ZKILLS = "Brain Munch"

AWARD_BAD_POINTS = "Piss Poor"
AWARD_BAD_KILLS = "Roleplayer"
AWARD_BAD_HEADSHOT = "Boom, Legshot"
AWARD_BAD_INFECT = "Cum Dumpster" 
AWARD_BAD_ZDEATH = "Meat Shield"

//Positive
Awards.AddToList({Name=AWARD_GOOD_POINTS, Desc="The player with the most "..GameInfo.PointName.."s.", CountStyle=HighToLow, Sound="bot/very_nice.wav"})
Awards.AddToList({Name=AWARD_GOOD_KILLS, Desc="The player with the most zombie kills.", CountStyle=HighToLow, Sound="bot/tag_them_and_bag_them.wav"})
Awards.AddToList({Name=AWARD_GOOD_HEADSHOT, Desc="The player with the most headshots.", CountStyle=HighToLow, Sound="bot/owned.wav"})
Awards.AddToList({Name=AWARD_GOOD_DISTANCE, Desc="The player with the longest distance kill (in feet).", CountStyle=HighToLow, Sound="bot/sniper.wav"})
Awards.AddToList({Name=AWARD_GOOD_ENGINEER, Desc="The engineer who made the most barricades.", CountStyle=HighToLow, Sound="bot/this_is_my_house.wav", Condition=Awards.Conditions.Engineer})
Awards.AddToList({Name=AWARD_GOOD_SUPPORT, Desc="The support who supplied the most ammo.", CountStyle=HighToLow, Sound="bot/good_job_team.wav", Condition=Awards.Conditions.Support})
Awards.AddToList({Name=AWARD_GOOD_MEDIC, Desc="The medic who healed the most teammates.", CountStyle=HighToLow, Sound="bot/i_got_your_back.wav", Condition=Awards.Conditions.Medic})
Awards.AddToList({Name=AWARD_GOOD_ITEMS, Desc="The player who gathered the most items.", CountStyle=HighToLow, Sound="bot/whoo2.wav"})
Awards.AddToList({Name=AWARD_GOOD_PYRO, Desc="The player who killed the most zombies with fire.", CountStyle=HighToLow, Sound="bot/i_am_on_fire.wav"})
Awards.AddToList({Name=AWARD_GOOD_ZKILLS, Desc="The zombie with the most kills.", CountStyle=HighToLow, Sound="npc/zombie/zombie_voice_idle2.wav", Condition=Awards.Conditions.Zomb})

//Negative
Awards.AddToList({Name=AWARD_BAD_POINTS, Desc="The player with the least amount of "..GameInfo.PointName.."s.", CountStyle=LowToHigh, Sound="bot/i_got_nothing.wav"})
Awards.AddToList({Name=AWARD_BAD_KILLS, Desc="The player who sat around with his finger up his ass.", CountStyle=LowToHigh, Sound="bot/nothing_happening_over_here.wav"})
Awards.AddToList({Name=AWARD_BAD_HEADSHOT, Desc="The person with the least headshots.", CountStyle=LowToHigh, Sound="bot/i_cant_see.wav"})
Awards.AddToList({Name=AWARD_BAD_INFECT, Desc="The human who was infected the most times.", CountStyle=HighToLow, Sound="bot/oh_man.wav"})
Awards.AddToList({Name=AWARD_BAD_ZDEATH, Desc="The zombie who stopped the most bullets with his head.", CountStyle=HighToLow, Sound="npc/zombie/zombie_die1.wav", Condition=Awards.Conditions.Zomb})

if (SERVER) then
	function Awards.Death(ply,attacker,dmginfo)
		if ply:Team() == TEAM_HUMANS then
			if attacker and attacker != NULL then
				if attacker:IsPlayer() then
					attacker:AddAwardPoints(AWARD_GOOD_ZKILLS,1)
				end
			end
		elseif ply:Team() == TEAM_UNDEAD then
			ply:AddAwardPoints(AWARD_BAD_ZDEATH,1)
		end
	end
	hook.Add("DoPlayerDeath","Awards_Death",Awards.Death)
end
